package string;

import java.util.Locale;
import java.util.Random;
import java.util.Scanner;

public class VerificationCodeDemo {
    public static void main(String[] args) {
        //用循环构建一个验证码范围池
        String codePool="";
        for (int i = 97; i < 26+97; i++) {
            codePool+=(char)i;
        }
        for (int i = 65; i < 26+65; i++) {
            codePool+=(char)i;
        }
        for (int i = 48; i < 10+48; i++) {
            codePool+=(char)i;
        }
//        System.out.println(codePool);
        //随机获取4个验证码
        Random ran=new Random();
        int temp=0;
        String code="";
        for (int i = 0; i < 4; i++) {
            temp=ran.nextInt(codePool.length());
            code+=codePool.charAt(temp);
        }
        System.out.println("Please enter the verification code below:");
        System.out.println(code);
        //用Scanner方法输入
        Scanner sc=new Scanner(System.in);
        String scan=sc.next();
        //变化大小写
        String Scan=scan.toUpperCase();
        String Code=code.toUpperCase();
        //用equals方法进行对比并输出结果
        if (Scan.equals(Code)){
            System.out.println("Verification succeeded!!");
        }else{

            while (true){
                System.out.println("Verification failed..Please re-enter:");
                temp=0;
                code="";
                for (int i = 0; i < 4; i++) {
                    temp=ran.nextInt(codePool.length());
                    code+=codePool.charAt(temp);
                }
                System.out.println(code);
                scan=sc.next();
                Scan=scan.toUpperCase();
                Code=code.toUpperCase();
                if (Scan.equals(Code)){
                    System.out.println("Verification succeeded!!");
                    break;
                }
            }

        }

    }
}
